<template>
  <div class="app-container">
    <div class="page-filter mb10 fl">
      <el-form ref="filter" :model="filter" label-width="0">
        <el-form-item class="mr10 mb5 fl">
          <el-input v-model="filter.nickname" placeholder="姓名" size="small" clearable />
        </el-form-item>
        <el-form-item class="mr10 mb5 fl">
          <el-button type="primary" size="small" @click="getList()">查 询</el-button>
          <el-button size="small" @click="app.reset('filter')">清 空</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table v-loading="app.loading" class="search-table" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="序号" align="center" width="56px" fixed="left">
        <template slot-scope="{$index}">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="姓名" min-width="120px" prop="nickname" />
      <el-table-column label="手机号" align="center" width="120px" prop="phone" />
      <el-table-column label="性别" align="center" width="80px" prop="sex" :formatter="sex" />
      <el-table-column label="系统角色" min-width="150px" prop="name" />
      <el-table-column label="邮箱" min-width="200px" prop="email" />
      <el-table-column label="状态" align="center" width="80px" prop="state">
        <template slot-scope="{row}">
          {{ row.state === 1 ? '启用' : '禁用' }}
        </template>
      </el-table-column>
      <el-table-column label="注册时间" align="center" width="150px" prop="register_time" />
      <el-table-column label="操作" align="center" width="90px" fixed="right">
        <template slot-scope="{row}">
          <el-dropdown>
            <el-button type="primary" size="mini">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item :disabled="row.id === '1' ? true : false" @click.native="editUser(row)">编 辑</el-dropdown-item>
              <el-dropdown-item @click.native="handleOffline(row)">下 线</el-dropdown-item>
              <el-dropdown-item @click.native="handleUpdate(row)">重 置</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="filter.pageAll>0" :total="filter.pageAll" :page.sync="filter.pageCurrent" :limit.sync="filter.pageNumber" @pagination="getList" />
    <Dialog ref="dialog" :get-data="getList" :info="dialog" />
  </div>
</template>

<script>
import { mngList, resetPTo, offline } from '@/api/user'
import { pass } from '@/api/auth'
import Pagination from '@/components/Pagination'
import Dialog from './components/user-dialog'

export default {
  name: 'User',
  components: { Pagination, Dialog },
  inject: ['app'],
  data() {
    return {
      list: null,
      dialog: {
        edit: {},
        title: '',
        type: ''
      },
      filter: {
        pageNumber: 10,
        pageCurrent: 1,
        pageAll: 1
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList(e) {
      this.app.loading = true
      mngList({
        ...this.filter,
        ...e
      }).then(response => {
        this.list = response.user
        this.filter = response.filter
        this.app.removeLoding()
      }).catch(error => {
        this.app.removeLoding()
        this.$message.error(error.msg ? error.msg : '操作失败')
      })
    },
    sex(row) {
      const s = row.sex
      if (s === 1) {
        return '男'
      } else if (s === 2) {
        return '女'
      } else {
        return '-'
      }
    },
    handleUpdate(row) {
      this.$confirm('此操作将重置用户密码, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        resetPTo({
          id: row['id']
        }).then(response => {
          this.$message.success('密码重置成功')
        })
      })
    },
    handleOffline(e) {
      offline({
        id: e['id']
      }).then(response => {
        pass()
        this.$message.success('操作成功')
      })
    },
    editUser(row) {
      this.dialog.title = '编辑管理员'
      this.dialog.type = 'edit'
      this.app.dialogParam(this.dialog.edit, ['id', 'username', 'nickname', 'phone', 'r_id', 'state', 'sex', 'email'], row)
      this.$refs['dialog'].visible = true
    }
  }
}
</script>
